package com.example.test003.controller;

import com.alibaba.fastjson.JSON;
import com.example.test003.pojo.WeatherOneDay;
import com.example.test003.service.WeatherOneDayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class WeatherController {

//    @Autowired
//    private WeatherService weatherService;

    @Autowired
    private WeatherOneDayService weatherOneDayService;

//    @GetMapping(value = "/queryAllWeathers", produces = {"application/json;charset=UTF-8"})
//    public String getAllBooks() {
//        List<Weather> weathers = weatherService.queryAllWeathers();
//        String Json_weather = JSON.toJSONString(weathers);
//        System.out.println(Json_weather);
//        return Json_weather;
//    }

//    @GetMapping(value = "/queryByCity", produces = {"application/json;charset=UTF-8"})
//    public String queryByName(String cityname) {
//        List<Weather> weathers = weatherService.queryByCity(cityname);
//        String Json_weather = JSON.toJSONString(weathers);
//        System.out.println(Json_weather);
//        return Json_weather;
//    }

    @GetMapping(value = "/queryAllWeathersOneDay", produces = {"application/json;charset=UTF-8"})
    public String getAllWeathersOneDay() {
        List<WeatherOneDay> weathers = weatherOneDayService.queryAllWeathersOneDay();
        String Json_weather = JSON.toJSONString(weathers);
        System.out.println(Json_weather);
        return Json_weather;
    }

    @GetMapping(value = "/queryAllWeathersOneDayByCity", produces = {"application/json;charset=UTF-8"})
    public String getAllWeathersOneDay(String cityname) {
        List<WeatherOneDay> weathers = weatherOneDayService.queryAllWeathersOneDayByCity(cityname);
        String Json_weather = JSON.toJSONString(weathers);
        System.out.println(weathers);
        System.out.println(Json_weather);
        boolean b = isJSON2(Json_weather);
        System.out.println(b);
        return Json_weather;
    }

//    @GetMapping(value = "queryKJYWeatherById", produces = {"application/json;charset=UTF-8"})
//    public String getKJYWeather(int universityid) {
//        List<WeatherOneDay> weathers = weatherOneDayService.queryKJYWeatherById(universityid);
//        String Json_weather = JSON.toJSONString(weathers);
//        System.out.println(Json_weather);
//        return Json_weather;
//    }

//    @GetMapping(value = "/queryWeatherPukou", produces = {"application/json;charset=UTF-8"})
//    public String getWeathersPukou() {
//        List<WeatherOneDay> weathers = weatherOneDayService.queryWeatherPukou();
//        String Json_weather = JSON.toJSONString(weathers);
//        System.out.println(Json_weather);
//        return Json_weather;
//    }

//    @GetMapping(value = "/queryWeatherTianchang", produces = {"application/json;charset=UTF-8"})
//    public String getWeathersTianchang() {
//        List<WeatherOneDay> weathers = weatherOneDayService.queryWeatherTianchang();
//        String Json_weather = JSON.toJSONString(weathers);
//        System.out.println(Json_weather);
//        return Json_weather;
//    }

//    @GetMapping(value = "/queryWeatherYuhuatai", produces = {"application/json;charset=UTF-8"})
//    public String getWeathersYuhuatai() {
//        List<WeatherOneDay> weathers = weatherOneDayService.queryWeatherYuhuatai();
//        String Json_weather = JSON.toJSONString(weathers);
//        System.out.println(Json_weather);
//        return Json_weather;
//    }

//    @GetMapping(value = "/queryWeatherLuhe", produces = {"application/json;charset=UTF-8"})
//    public String getWeathersLuhe() {
//        List<WeatherOneDay> weathers = weatherOneDayService.queryWeatherLuhe();
//        String Json_weather = JSON.toJSONString(weathers);
//        System.out.println(Json_weather);
//        return Json_weather;
//    }


//    @GetMapping(value = "queryPukouNow", produces = {"application/json;charset=UTF-8"})
//    public String getPukouNow() {
//        LocalTime time = LocalTime.now(); // get the current time
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH");
//        String nowTime = time.format(formatter);
//        System.out.println(nowTime);//
//        WeatherOneDay weatherOneDay = weatherOneDayService.queryPukouNow(nowTime);
//        String Json_weather = JSON.toJSONString(weatherOneDay);
//        return Json_weather;
//    }

//    @GetMapping(value = "queryLuheNow", produces = {"application/json;charset=UTF-8"})
//    public String getLuheNow() {
//        LocalTime time = LocalTime.now(); // get the current time
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH");
//        String nowTime = time.format(formatter);
//        System.out.println(nowTime);//
//        WeatherOneDay weatherOneDay = weatherOneDayService.queryLuheNow(nowTime);
//        String Json_weather = JSON.toJSONString(weatherOneDay);
//        return Json_weather;
//    }

//    @GetMapping(value = "queryYuhuataiNow", produces = {"application/json;charset=UTF-8"})
//    public String getYuhuataiNow() {
//        LocalTime time = LocalTime.now(); // get the current time
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH");
//        String nowTime = time.format(formatter);
//        System.out.println(nowTime);//
//        WeatherOneDay weatherOneDay = weatherOneDayService.queryYuhuataiNow(nowTime);
//        String Json_weather = JSON.toJSONString(weatherOneDay);
//        return Json_weather;
//    }

//    @GetMapping(value = "queryTianchangNow", produces = {"application/json;charset=UTF-8"})
//    public String getTianchangNow() {
//        LocalTime time = LocalTime.now(); // get the current time
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH");
//        String nowTime = time.format(formatter);
//        System.out.println(nowTime);//
//        WeatherOneDay weatherOneDay = weatherOneDayService.queryTianchangNow(nowTime);
//        String Json_weather = JSON.toJSONString(weatherOneDay);
//        return Json_weather;
//    }


//    @GetMapping(value = "queryWindJson")
//    public String getWindJson(String day, String time) throws IOException {
//        Calendar calendar = Calendar.getInstance();
//        int hour = calendar.get(Calendar.HOUR_OF_DAY); // 24小时制
//
//        //获取昨天的日期
//        Calendar cal = Calendar.getInstance();
//        cal.add(Calendar.DATE, -1);
//        String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
//
//        if (hour <= 14) {
//            day = yesterday;
//        }
//
//        if (Integer.valueOf(time) < 10) {
//            String path = "C:\\Users\\Cold'Rain\\Desktop\\test003\\src\\main\\resources\\python\\figure\\";
//            String path1 = path + day + "\\" + time + "\\wind.json";
//            String fileName = "/Users/pankaj/source.txt";
//            File file = new File(path1);
//            FileInputStream fis = new FileInputStream(file);
//            InputStreamReader isr = new InputStreamReader(fis);
//            BufferedReader br = new BufferedReader(isr);
//
//            String data = "";
//            String line;
//            while ((line = br.readLine()) != null) {
//                //process the line
//                //System.out.println(line);
//                data += line;
//            }
//            br.close();
//            String allJson = JSON.toJSONString(data);
//            return allJson;
//        } else if (Integer.valueOf(time) < 100) {
//            String path = "C:\\Users\\Cold'Rain\\Desktop\\test003\\src\\main\\resources\\python\\figure\\";
//            String path1 = path + day + "\\" + time + "\\wind.json";
//            String fileName = "/Users/pankaj/source.txt";
//            File file = new File(path1);
//            FileInputStream fis = new FileInputStream(file);
//            InputStreamReader isr = new InputStreamReader(fis);
//            BufferedReader br = new BufferedReader(isr);
//
//            String data = "";
//            String line;
//            while ((line = br.readLine()) != null) {
//                //process the line
//                //System.out.println(line);
//                data += line;
//            }
//            br.close();
//            String allJson = JSON.toJSONString(data);
//            return allJson;
//        } else {
//            String path = "C:\\Users\\Cold'Rain\\Desktop\\test003\\src\\main\\resources\\python\\figure\\";
//            String path1 = path + day + "\\" + time + "\\wind.json";
//            String fileName = "/Users/pankaj/source.txt";
//            File file = new File(path1);
//            FileInputStream fis = new FileInputStream(file);
//            InputStreamReader isr = new InputStreamReader(fis);
//            BufferedReader br = new BufferedReader(isr);
//
//            String data = "";
//            String line;
//            while ((line = br.readLine()) != null) {
//                //process the line
//                //System.out.println(line);
//                data += line;
//            }
//            br.close();
//            String allJson = JSON.toJSONString(data);
//            return allJson;
//        }
//    }
//
//    @RequestMapping(value = "/say1")
//    public @ResponseBody
//    String say1() {
//
//        return "Hello,SpringBoot! in weather";
//    }


    //创建科技园简介txt
//    @GetMapping(value = "/wuhu")
//    public void createTxt() throws IOException {
//        List<Kejiyuan> kejiyuans=weatherOneDayService.test();
//        for (int i = 0; i < kejiyuans.size(); i++) {
//            String path="C:/Users/1/Desktop/KJY/"+kejiyuans.get(i).getUniversityname()+".txt";
//            File file=new File(path);
//            file.createNewFile();
//        }
//        System.out.println(1);
//    }

    //判断是否为json格式
    public static boolean isJSON2(String str) {
        boolean result = false;
        try {
            Object obj = JSON.parse(str);
            result = true;
        } catch (Exception e) {
            result = false;
        }
        return result;
    }
}

